/*!
    \file    gd32l23x_it.c
    \brief   interrupt service routines

    \version 2021-08-15, V1.0.0, demo for GD32L23x
*/

/*
    Copyright (c) 2021, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "gd32l23x_it.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "Caige.h"
#include "gd32l23x_eval.h"
#include "led.h"
#include "flash.h"
#include "tim.h"
#include "device.h"

extern SemaphoreHandle_t binary_semaphore;
extern SemaphoreHandle_t exti0_semaphore;
extern SemaphoreHandle_t exti1_semaphore;
extern SemaphoreHandle_t dma0_semaphore;
extern SemaphoreHandle_t dma1_semaphore;
//extern SemaphoreHandle_t rate_speed_semaphore;
//extern SemaphoreHandle_t rate_semaphore;


//extern SemaphoreHandle_t xserialMutex;
/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void NMI_Handler(void)
{
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void HardFault_Handler(void)
{
    /* if Hard Fault exception occurs, go to infinite loop */
    while(1) {
    }
}

/*!
    \brief      this function handles external lines 10 to 15 interrupt request
    \param[in]  none
    \param[out] none
    \retval     none
*/

#if 0

void EXTI0_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken;

    if(RESET != exti_interrupt_flag_get(EXTI_0)) {
        nvic_irq_disable(KEY_IRQn[KEY_WAKEUP]);

        if(NULL != exti0_semaphore) {
            /* release binary semaphores. */
            xSemaphoreGiveFromISR(exti0_semaphore, &xHigherPriorityTaskWoken);
            /* make a task switch if necessary. */
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
        }

        exti_interrupt_flag_clear(EXTI_0);
    }
}

void EXTI1_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;

    if(RESET != exti_interrupt_flag_get(EXTI_1)) {
        nvic_irq_disable(KEY_IRQn[KEY_TAMPER]);

        if(NULL != exti1_semaphore) {
            /* release binary semaphores. */
            xSemaphoreGiveFromISR(exti1_semaphore, &xHigherPriorityTaskWoken);
            /* make a task switch if necessary. */
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
        }

        exti_interrupt_flag_clear(EXTI_1);
    }
}



void EXTI10_15_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken;

    if(RESET != exti_interrupt_flag_get(EXTI_13)) {
        if(NULL != binary_semaphore) {
            /* release binary semaphores. */
            xSemaphoreGiveFromISR(binary_semaphore, &xHigherPriorityTaskWoken);
            /* make a task switch if necessary. */
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
        }

        exti_interrupt_flag_clear(EXTI_13);
    }
}
#endif

void EXTI5_9_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken;

    if(RESET != exti_interrupt_flag_get(EXTI_6)) {
        if(NULL != binary_semaphore) {
            /* release binary semaphores. */
            xSemaphoreGiveFromISR(binary_semaphore, &xHigherPriorityTaskWoken);
            /* make a task switch if necessary. */
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
        }

        exti_interrupt_flag_clear(EXTI_6);
    }
}


#ifdef DEV_BOARD

static uint8_t aa;

#endif

#if 1
void USART0_IRQHandler(void)
{
    portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;

    if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
        /* read one byte from the receive data register */
        // rx_buffer[rx_counter++] = (uint8_t)usart_data_receive(USART0);
        // rx_counter &= (BUFFER_SIZE - 1);
#ifdef DEV_BOARD
        if(aa) {
            gd_eval_led_on(LED4);
            aa = 0;

        } else {
            aa = 1;
            gd_eval_led_off(LED4);
        }

#endif
        //if (xSemaphoreTake(xserialMutex, 10) == pdTRUE){
        // rx_ok = 1;
        usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
        //xSemaphoreGive(xserialMutex);
        //}else{
        //}
        //if(rx_ok) {
        //rx_counter = 0;
        //rx_ok=1;
        //xQueueSendFromISR( xRxedChars, rx_buffer, &xHigherPriorityTaskWoken );
        /* disable the USART0 receive interrupt */
        //    usart_interrupt_disable(USART0, USART_INT_RBNE);
        //}
    }

#if 1

    if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)) {
        /* write one byte to the transmit data register */
        //usart_data_transmit(USART1, tx_buffer[tx_counter++]);
        //if(tx_counter >= nbr_data_to_send) {
        /* disable the USART0 transmit interrupt */
        //    usart_interrupt_disable(USART0, USART_INT_TBE);
        //}
    }

#endif

    if(xHigherPriorityTaskWoken) {
        /* Actual macro used here is port specific. */
        //taskYIELD_FROM_ISR ();
    }
}
#endif
void DMA_Channel0_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken;

    if(RESET != dma_interrupt_flag_get(DMA_CH0, DMA_INT_FLAG_FTF)) {
        //dma_interrupt_flag_clear(DMA_CH0, DMA_INT_FLAG_FTF);
        dma_interrupt_flag_clear(DMA_CH0, DMA_INT_FLAG_G);
        adc_disable();

        //g_transfer_complete = SET;
        if(NULL != dma0_semaphore) {
            /* release binary semaphores. */
            xSemaphoreGiveFromISR(dma0_semaphore, &xHigherPriorityTaskWoken);
            /* make a task switch if necessary. */
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
        }
    }
}
void DMA_Channel1_IRQHandler(void)
{
    BaseType_t xHigherPriorityTaskWoken;

    if(RESET != dma_interrupt_flag_get(DMA_CH1, DMA_INT_FLAG_FTF)) {
        //dma_interrupt_flag_clear(DMA_CH0, DMA_INT_FLAG_FTF);
        dma_interrupt_flag_clear(DMA_CH1, DMA_INT_FLAG_G);
        //adc_disable();

        //g_transfer_complete = SET;
        if(NULL != dma1_semaphore) {
            /* release binary semaphores. */
            xSemaphoreGiveFromISR(dma1_semaphore, &xHigherPriorityTaskWoken);
            /* make a task switch if necessary. */
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
        }
    }
}







#ifdef DEBUG_TMP


#define LEN_BUF_TMP 128
#define LEN_BUF_TMP_MASK (LEN_BUF_TMP-1)
uint32_t buf_tmp[LEN_BUF_TMP];
uint8_t buf_tmp_i;
#define RATE_OR_SPEED 1
//1:rate 0:speed
void print_debug_tmp()
{
    static uint8_t k = 0;
    uint8_t i;

    if(k++ < 5) {
        return;
    }

    k = 0;

    for(i = 0; i < buf_tmp_i; i++) {
        log_debug("=0x%x=\t", buf_tmp[i]);
        buf_tmp[i] = 0;

        if((i % 2) == 1) {
            log_debug("\n");
        }
    }

    log_debug("@%d@\n", buf_tmp_i);
    buf_tmp_i = 0;
}

#endif


void FMC_IRQHandler()
{
    if(fmc_interrupt_flag_get(FMC_INT_FLAG_END)) {
        flash_st.int_flag = 1;
        fmc_interrupt_flag_clear(FMC_INT_FLAG_END);
    }
}